package com.yyk.yiliao.ui.shop.activity;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;

import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.ui.activity.detail.activity.Detail_Activity;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.bean.HomeHomegoods_Info;
import com.yyk.yiliao.util.rx.RxUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 搜索药品
 */
public class Hospital_Search_Activity extends BaseActivity {

    @BindView(R.id.mSearch_int)
    EditText mSearchInt;
    @BindView(R.id.mSearch_clean)
    ImageView mSearchClean;
    @BindView(R.id.mSearch_sousuo)
    TextView mSearchSousuo;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.mSearch_rv)
    RecyclerView mSearchRv;
    @BindView(R.id.mSearch_no)
    TextView mSearchNo;
    @BindView(R.id.ll_center_bg)
    LinearLayout llCenterBg;
    @BindView(R.id.mSearch)
    LinearLayout mSearch;
    private BaseRecyclerAdapter<HomeHomegoods_Info.DataBean> adapter;
    private String request;
    private List<HomeHomegoods_Info.DataBean> data = new ArrayList<>();


    @Override
    public int getLayoutId() {
        return R.layout.activity_hosptila_search;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    /**
     * TODO Toolbar去除标题与返回键间的间距 - 简书  https://www.jianshu.com/p/0c3a94d00f5f
     * <p>
     * 解决了 返回 和 Title 之间的间距问题
     */
    private void initToolbar() {
        hidetoolBar();
        toolbar.setNavigationIcon(getResources().getDrawable(R.mipmap.icon_shangp_fh));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void initData() {
        initAdapter();

    }

    private void initAdapter() {
        adapter = new BaseRecyclerAdapter<HomeHomegoods_Info.DataBean>(Hospital_Search_Activity.this, data, R.layout.adapter_item_hospitaldoctor) {
            @Override
            public void convert(BaseRecyclerHolder holder, HomeHomegoods_Info.DataBean item, int position, boolean isScrolling) {
                String name = item.getName(); //搜索后的名字
                holder.setText(R.id.b_hospital_name, name);
            }
        };
        mSearchRv.setLayoutManager(new LinearLayoutManager(Hospital_Search_Activity.this, LinearLayoutManager.VERTICAL, false));
        mSearchRv.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                int id = data.get(position).getId();//商品Id
                if (id != 0) {
                    //TODO 跳到  商品详情
//                    Intent intent = new Intent(Hospital_Search_Activity.this, Detail_Activity.class);
//                    EventBus.getDefault().postSticky(new MessageEvent(id + ""));
//                    startActivity(intent);
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", data.get(position).getId());
                    startAct(Detail_Activity.class, bundle);
                }
            }
        });
    }

    /**
     * 文本框搜索医院或者医生   连续搜索接口有问题
     */
    private void setmSearch(Map<String, String> map, final boolean flag) {
        RxUtils.getInstance().retrofit.create(ApiService.class).postHomeHomegoods(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeHomegoods_Info>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
//                        ToastUtil.showShort(Hospital_Search_Activity.this, "您点击轻点...");
                    }

                    @Override
                    public void onNext(HomeHomegoods_Info info) {

                        Logger.e("商城搜索" + info.toString());
                        if (info.getCode() == 1) {
                            //                            mSearchRv.setVisibility(View.VISIBLE);
                            //                            llCenterBg.setVisibility(View.GONE);

                            if (flag) {
                                data.clear();
                            }

                            if (null != info.getData() && info.getData().size() > 0) {
                                data.addAll(info.getData());
                            }
                            adapter.notifyDataSetChanged();

                        } else {
                            //                            mSearchRv.setVisibility(View.GONE);
                            //                            llCenterBg.setVisibility(View.VISIBLE);
                            ToastUtil.showShort(Hospital_Search_Activity.this, "没有找到这件商品");
                        }

                    }
                });
    }


    @OnClick({R.id.mSearch_int, R.id.mSearch_clean, R.id.mSearch_sousuo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mSearch_int:
                break;
            case R.id.mSearch_clean:
                mSearchInt.setText("");
                break;
            case R.id.mSearch_sousuo:
                request = mSearchInt.getText().toString().trim();

                Map<String, String> map = new TreeMap<>();
                map.put("name", request);
                map.put("sign", Md5Util2.createSign(map));
                Logger.e("name___________", request);
                setmSearch(map, true);
                break;
        }
    }


}
